
import 'package:flutter_wan_android/data/logic_result.dart';
import 'package:flutter_wan_android/data/user/user_repository.dart';
import 'package:flutter_wan_android/feature/core/base_view_model.dart';

class RegisterViewModel extends BaseViewModel {

  final UserRepository _repository;

  RegisterViewModel(this._repository);

  Future<void> register(String account,String password,String rePassword) async{
    if(account.isEmpty){
      notifyError("请输入账号");
    }else if (password.isEmpty){
      notifyError("请输入密码");
    }else if (rePassword.isEmpty){
      notifyError("请再次输入密码");
    }else if (password != rePassword){
      notifyError("两次密码输入不一致");
    }else {
      notifyLoading();
      final result = await _repository.register(account, password, rePassword);
      switch(result){
        case Ok():
          notifySuccess();
        case Fail(:final code, :final msg):
          notifyError(msg,code: code);
      }
    }
  }
}